package guoanmaker.operator.business.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.dao.OperatorEventDetailsDao;
import guoanmaker.operator.business.dao.OperatorLuckDrawDao;
import guoanmaker.operator.business.dao.OperatorProviderOrderDao;
import guoanmaker.operator.business.dao.SecckillCommodityDao;
import guoanmaker.operator.business.model.OperatorActivityApplication;
import guoanmaker.operator.business.model.OperatorEventDetails;
import guoanmaker.operator.business.model.OperatorLuckdraw;
import guoanmaker.operator.business.model.OperatorSecckill;
import guoanmaker.operator.business.modelnobind.LuckDrawAndCoupons;
import guoanmaker.operator.business.modelnobind.OderFormRo;
import guoanmaker.operator.business.modelnobind.SkillAndCommdMove;
import guoanmaker.operator.business.modelnobind.operatorEnlistDetails;
import guoanmaker.operator.business.repository.OperatorActivityApplicationRepository;
import guoanmaker.operator.business.repository.OperatorActivityTypeRepository;
import guoanmaker.operator.business.repository.OperatorEventDetailsRepository;
import guoanmaker.operator.business.repository.OperatorLuckdrawRepository;
import guoanmaker.operator.business.repository.OperatorSecckillRepository;

@Service
public class OperatorEventDetailsService implements OperatorEventDetailsServiceInterface {

	private final OperatorEventDetailsRepository operatorEventDetailsRepository;
	private final OperatorActivityApplicationRepository operatorActivityApplicationRepository;
	private final OperatorLuckdrawRepository operatorLuckdrawRepository;
	private final OperatorEventDetailsDao operatorEventDetailsDao;
	private final OperatorLuckDrawDao operatorLuckDrawDao;
	private final OperatorSecckillRepository operatorSecckillRepository;
	private final SecckillCommodityDao secckillCommodityDao;
	@Autowired
	private OperatorProviderOrderDao  operatorProviderOrderDao;
	public OperatorEventDetailsService(OperatorEventDetailsRepository operatorEventDetailsRepository,
			OperatorActivityTypeRepository operatorActivityTypeRepository,
			OperatorActivityApplicationRepository operatorActivityApplicationRepository,
			OperatorLuckdrawRepository operatorLuckdrawRepository, OperatorEventDetailsDao operatorEventDetailsDao,OperatorLuckDrawDao operatorLuckDrawDao,
			OperatorSecckillRepository operatorSecckillRepository,SecckillCommodityDao secckillCommodityDao) {
		this.operatorEventDetailsRepository = operatorEventDetailsRepository;
		this.operatorActivityApplicationRepository = operatorActivityApplicationRepository;
		this.operatorLuckdrawRepository = operatorLuckdrawRepository;
		this.operatorEventDetailsDao = operatorEventDetailsDao;
		this.operatorLuckDrawDao=operatorLuckDrawDao;
		this.operatorSecckillRepository=operatorSecckillRepository;
		this.secckillCommodityDao=secckillCommodityDao;
	}

	@Override
	public List<Object> getSelectEvent(String id) {
		List<Object> list = new ArrayList<Object>();
		OperatorEventDetails eventDetails = operatorEventDetailsDao.getSelectEvent(id);
		if(eventDetails!=null){
			list.add(eventDetails);
			if (eventDetails.getActivityTypeId().equals("抽奖") == true) {
				List<LuckDrawAndCoupons> listLuckdraw =operatorLuckDrawDao.queryAllLuckdrawByActivityId(id);
				list.add(listLuckdraw);
			}
			if(eventDetails.getActivityTypeId().equals("秒杀") == true){
				List<SkillAndCommdMove>  operatorSecckill =	secckillCommodityDao.selectSkillAndCommdMoveByActivityId(id);
				list.add(operatorSecckill);
			}
			else {
				List<OperatorActivityApplication> listApplication = operatorActivityApplicationRepository
						.queryActivityApplicationList(id);
				list.add(listApplication);
			}
		}
		return list;
	}

	@Override
	public OperatorEventDetails saveNewOperatorEventDetails(OperatorEventDetails operatorEventDetails) {
		operatorEventDetails.setEventJoinPersonNum(0);
		return operatorEventDetailsRepository.save(operatorEventDetails);
	}

	@Override
	public void delete(String id) {
		operatorEventDetailsRepository.updateStateDelete(id);
	}

	@Override
	public void updateState(OperatorEventDetails operatorEventDetails, String id) {
		// 修改活动结束时间
		operatorEventDetails.setEventEndTime(new Date());
		operatorEventDetailsRepository.endEvent(operatorEventDetails.getEventEndTime(), id);
		
	}

	@Override
	public OperatorEventDetails saveEvent(OperatorEventDetails OperatorEventDetails) {
		OperatorEventDetails.setEventJoinPersonNum(0);
		if(OperatorEventDetails.getState()==null){
			if(OperatorEventDetails.getAvailability()==0){//未生效
				OperatorEventDetails.setState(7);
			 }
			if(OperatorEventDetails.getAvailability()==1){//生效
				OperatorEventDetails.setState(0);
			 }
		}
		return operatorEventDetailsRepository.save(OperatorEventDetails);
	}

	@SuppressWarnings("unused")
	@Override
	public Map<String, Object> LikeSelect(String eventName, Integer eventType, Integer page, Integer size) {
		
		Map<String, Object> likeSelect = operatorEventDetailsDao.LikeSelect(eventName, eventType, page, size);
		for (String key : likeSelect.keySet()) {
			@SuppressWarnings("unchecked")
			List<OperatorEventDetails> object = (List<OperatorEventDetails>) likeSelect.get("cuont");
			for (OperatorEventDetails operatorEventDetails : object) {
				if(operatorEventDetails.getActivityTypeId().equals("秒杀")==true){
					Integer num=operatorProviderOrderDao.querycountnum(operatorEventDetails.getId());
					
					if(num==null){
						num=0;
					}
					operatorEventDetails.setActivePayment(num);
				}
				else{
					operatorEventDetails.setActivePayment(operatorEventDetailsDao.getEventJoinPersonNums(operatorEventDetails.getId(), operatorEventDetails.getActivityTypeId()));
				}
			}
		}
		return likeSelect;
	}

	@SuppressWarnings({ "unchecked", "unused" })
	@Override
	public Map<String, Object> multipleConditionQuery(Integer EventType, String eventTitle, Integer state,
			String founder, Integer isMoney, String stratTime, String endTime, Integer page, Integer size) {
		String EndTimes = null;
		String StratTimes = null;

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		if (stratTime != null && endTime != null) {

			long parseLong = Long.parseLong(stratTime);// String类型的毫秒值
			long parseLong2 = Long.parseLong(endTime);

			Date StratTime = new Date(parseLong);// Long转换Date
			Date EndTime = new Date(parseLong2);

			StratTimes = format.format(StratTime);// 转换时间类型成yyyy-mm-dd hh：mm：ss
			EndTimes = format.format(EndTime);

		} else if (stratTime != null && endTime == null) {

			long parseLong = Long.parseLong(stratTime);
			Date StratTime = new Date(parseLong);
			StratTimes = format.format(StratTime);

		} else if (stratTime == null && endTime != null) {

			long parseLong2 = Long.parseLong(endTime);
			Date EndTime = new Date(parseLong2);
			EndTimes = format.format(EndTime);
		}
		
		Map<String, Object> multipleConditionQuery = operatorEventDetailsDao.multipleConditionQuery(EventType, eventTitle, state, founder, isMoney,StratTimes, EndTimes, page, size);
		
		for (String key : multipleConditionQuery.keySet()) {
			List<OperatorEventDetails> object = (List<OperatorEventDetails>) multipleConditionQuery.get("cuont");
			for (OperatorEventDetails operatorEventDetails : object) {
				if(operatorEventDetails.getActivityTypeId().equals("秒杀")==true){
					Integer num=operatorProviderOrderDao.querycountnum(operatorEventDetails.getId());
					if(num==null){
						num=0;
					}
					operatorEventDetails.setActivePayment(num);
					}
				else{
					operatorEventDetails.setActivePayment(operatorEventDetailsDao.getEventJoinPersonNums(operatorEventDetails.getId(), operatorEventDetails.getActivityTypeId()));
				}
		    }
		}
		
		return multipleConditionQuery;
	}

	@SuppressWarnings({ "unchecked", "unused" })
	@Override
	public Map<String, Object> getAllOperatorEventDetails(Integer eventType, Integer page, Integer size) {

		Map<String, Object> allOperatorEventDetails = operatorEventDetailsDao.getAllOperatorEventDetails(eventType,page, size);

		for (String key : allOperatorEventDetails.keySet()) {
			List<OperatorEventDetails> object = (List<OperatorEventDetails>) allOperatorEventDetails.get("cuont");
			for (OperatorEventDetails operatorEventDetails : object) {
				if(operatorEventDetails.getActivityTypeId().equals("秒杀")==true){
					Integer num=operatorProviderOrderDao.querycountnum(operatorEventDetails.getId());
					if(num==null){
						num=0;
					}
					operatorEventDetails.setActivePayment(num);
					}
				else{
					operatorEventDetails.setActivePayment(operatorEventDetailsDao.getEventJoinPersonNums(operatorEventDetails.getId(), operatorEventDetails.getActivityTypeId()));
				}
			}
		}
		return allOperatorEventDetails;
	}

	/**
	 * 不分上下线查询所有活动但是已删除和未发布不能被查询出来
	 */
	@Override
	public List<OperatorEventDetails> getSelectEventAll() {
		return operatorEventDetailsRepository.selectEventAll();
	}

	@Override
	public Map<String, Object> getselectByEventId(String id, Integer page, Integer size) {

		return operatorEventDetailsDao.getEventById(id, page, size);
	}

	@Override
	public Map<String, Object> getEventEnrollment(String id, String activityTypeId, Integer page, Integer size) {

		return operatorEventDetailsDao.getEventUser(id, activityTypeId, page, size);
	}

	@Override
	public Map<String, Object> getAppAndIosAndPcAllOperatorEventDetails(Integer EventType, Integer page, Integer size) {
		return operatorEventDetailsDao.getAppAndIosAndPc(EventType, page, size);
	}

	@Override
	public Map<String, Object> exportEventDetailsOfApplicants(String id,String activityTypeId) {
		return operatorEventDetailsDao.getEventDetailsOfApplicants(id,activityTypeId);
	}

	@Override
	public Boolean theQueryActivityTitleIsRepeated(String eventTitle) {
		
		return operatorEventDetailsDao.theQueryActivityTitleIsRepeated(eventTitle);
	}

	@Override
	public String selectSecckillEventDetails() {
		return operatorEventDetailsDao.selectSecckillEventDetails();
	}
}